package com.wenhao.dewu;

import com.wenhao.leetcode.structure.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wenhao Tong
 * @create 2021-08-29 19:07
 */
public class Main1 {
    public static void main(String[] args) {

    }
    /**
     *
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        while (head1 != null) {
            list1.add(head1.val);
            head1 = head1.next;
        }
        while (head2 != null) {
            list2.add(head2.val);
            head2 = head2.next;
        }
        int i1 = list1.size() - 1;
        int i2 = list2.size() - 1;
        ListNode head = new ListNode();
        int up = 0;
        while (i1 > -1 || i2 > -1) {
            int temp = 0;
            if (i1 > -1 && i2 > -1) {
                temp = list1.get(i1) + list2.get(i2) + up;
            } else if (i1 > -1) {
                temp = list1.get(i1) + up;
            } else if (i2 > -1) {
                temp = list2.get(i2) + up;
            }
            up = temp / 10;
            ListNode node = new ListNode(temp % 10);
            node.next = head.next;
            head.next = node;
            i1--;
            i2--;
        }
        if (up > 0) {
            ListNode node = new ListNode(1);
            node.next = head.next;
            head.next = node;
        }
        return head.next;
    }
}
